package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.pojo.TbOrder;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/18 17:42
 */
public class OrderDaoImpl implements OrderDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public void insertOrder(String orderId, Long uid, long sum, long aid) {
        String sql="insert into tb_order(id,uid,money,status,aid) values (?,?,?,'1',?)";
        try {
            queryRunner.update(sql,orderId,uid,sum,aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addOrderDetail(String orderId, long pid, long num, long money) {
        String sql="insert into tb_orderdetail(Oid,pid,num,Money) values (?,?,?,?)";
        try {
            queryRunner.update(sql,orderId,pid,num,money);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<TbOrder> selectOrder(Long uid) {

        String sql="select * from tb_order where uid=?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(TbOrder.class),uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TbOrder selectById(String orderId) {
        String sql="select * from tb_order where id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(TbOrder.class),orderId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void udateStatus(String oid, int status) {
        String sql="update tb_order set status=2 where id=?";
        try {
            queryRunner.update(sql,oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
